<?php
class ApiPosterController extends BpfController
{
  public $allows = ['report', 'recommend'];

  public function reportAction()
  {
    $playerModel = $this->getModel('player');
    $player =  $playerModel->getPlayer($this->req->getQueryTrim('player_id'));
    if ($player) {
      $datas = [];
      $statisticModel = $this->getModel('statistic');
      foreach ($statisticModel->last7Flower(2) as $data) {
        $datas[] = $data->flowers;
      }
      $statisticModel = $this->getModel('statistic');
      $lastWeek = date('YW', REQUEST_TIME - 604800);
      $this->getModel('poster')->report([
        'name' => $player->nickname,
        'player_id' => $player->player_id,
        'datas' => $datas,
        'visitor' => $player->getVisitorWeekCount($lastWeek),
        'flowers' => $player->getflowersWeekCount($lastWeek),
        'allrank' => $statisticModel->getAllRank($player->player_id),
      ]);
    }
  }

  public function recommendAction()
  {
    $playerModel = $this->getModel('player');
    $player =  $playerModel->getPlayer($this->req->getQueryTrim('player_id'));
    if ($player) {
      $datas = [];
      $statisticModel = $this->getModel('statistic');
      foreach ($statisticModel->last7Flower(2) as $data) {
        $datas[] = $data->flowers;
      }
      $statisticModel = $this->getModel('statistic');
      $lastWeek = date('YW', REQUEST_TIME - 604800);
      $this->getModel('poster')->recommend([
        'player_id' => $player->player_id,
        'name' => $player->nickname,
        'store' => $player->getStore()->data ?? []
      ]);
    }
  }
}
